﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

using VirtualTournaments.Business.Tournaments;

namespace VirtualTournaments.DataAccess.SqlServer.DataProxies
{
	internal class PlayOffTournamentDataProxy : Business.Tournaments.PlayOffTournament, IDataProxy<SqlServer.Tournament>, Identifiable<SqlServer.Tournament>
	{
		private SqlServer.Tournament _dataObject;

		internal PlayOffTournamentDataProxy(SqlServer.Tournament dataObject, IEnumerable<Business.Tournaments.Team> teams)
			: base(teams)
		{
			this._dataObject = dataObject;
			Name = dataObject.Name;
			IsFinished = dataObject.IsFinished;
			IsActive = dataObject.IsActive;
		}

		private bool _firstHasBeenRead = false;
		private bool _secondHasBeenRead = false;
		private bool _thirdHasBeenRead = false;
		private bool _fixtureHasBeenRead = false;

		public override Business.Tournaments.Team First
		{
			get
			{
				if (!_firstHasBeenRead)
				{
					_firstHasBeenRead = true;
					if (base.First == null)
					{
						//base.First = TeamsDataMapper.Instance.DataObject2BusinessObject(_dataObject.FirstTeam);
						throw new System.NotImplementedException();
					}
				}
				return base.First;
			}
		}

		public override Business.Tournaments.Team Second
		{
			get
			{
				if (!_secondHasBeenRead)
				{
					_secondHasBeenRead = true;
					if (base.Second == null)
					{
						//base.Second = TeamsDataMapper.Instance.DataObject2BusinessObject(_dataObject.SecondTeam);
						throw new System.NotImplementedException();
					}
				}
				return base.Second;
			}
		}

		public override Business.Tournaments.Team Third
		{
			get
			{
				if (!_thirdHasBeenRead)
				{
					_thirdHasBeenRead = true;
					if (base.Third == null)
					{
						//base.Third = TeamsDataMapper.Instance.DataObject2BusinessObject(_dataObject.ThirdTeam);
						throw new System.NotImplementedException();
					}
				}
				return base.Third;
			}
		}

		public override Business.Tournaments.Fixture Fixture
		{
			get
			{
				if (!_fixtureHasBeenRead)
				{
					_fixtureHasBeenRead = true;
					if (base.Fixture == null)
					{
						//base.Fixture = MapFixture(_dataObject);
						throw new System.NotImplementedException();
					}
				}
				return base.Fixture;
			}
		}

		private Business.Tournaments.Fixture MapFixture(SqlServer.Tournament dataObject)
		{
			throw new System.NotImplementedException();

			//List<Journey> journeys = new List<Journey>();
			//foreach (SqlServer.Journey journey in dataObject.Journeys)
			//{
			//    //journeys.Add(JourneysDataMapper.Instance.DataObject2BusinessObject(journey));
			//    throw new System.NotImplementedException();
			//}
			//Business.Tournaments.Fixture fixture = new Business.Tournaments.Fixture(this);
			//return fixture;
		}

		#region IDataProxy<Tournament> Members

		public SqlServer.Tournament InnerDataObject
		{
			get { return this._dataObject; }
			set { this._dataObject = value; }
		}

		#endregion

		#region Identifiable<Tournament> Members

		public Expression<Func<SqlServer.Tournament, bool>> GetUniqueWhereExpression()
		{
			return tournament => tournament.TournamentId == InnerDataObject.TournamentId;
		}

		#endregion
	}
}
